//
//  YSContentView.m
//  YSPageViewExample_OC
//
//  Created by MOLBASE on 2017/12/5.
//  Copyright © 2017年 YangShen. All rights reserved.
//

#import "YSContentView.h"
#import "YSPageStyle.h"
#import "YSTitleView.h"
#import "UIView+YSFrame.h"
#import "UIColor+YSExtension.h"

static NSString * const kContentCellID = @"kContentCellID";

@interface YSContentView () <UICollectionViewDelegate, UICollectionViewDataSource, YSTitleViewDelegate>

@property (nonatomic, strong) NSArray<UIViewController *> *childVcArray;
@property (nonatomic, strong) YSPageStyle *style;
@property (nonatomic, weak  ) UIViewController *parentVc;

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) BOOL isAniming;

@end

@implementation YSContentView

- (instancetype)initWithFrame:(CGRect)frame childVcs:(NSArray<UIViewController *> *)childVcArray parentVc:(UIViewController *)parentVc style:(YSPageStyle *)style {
    if (self = [super initWithFrame:frame]) {
        self.childVcArray = childVcArray;
        self.parentVc = parentVc;
        self.style = style;
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    for (UIViewController *vc in self.childVcArray) {
        [self.parentVc addChildViewController:vc];
    }
    
    [self addSubview:self.collectionView];
    self.collectionView.contentOffset = CGPointMake(self.style.selectIndex * self.collectionView.ys_width, 0);
}

#pragma mark -
#pragma mark 懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.itemSize = self.bounds.size;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionView.pagingEnabled = YES;
        _collectionView.bounces = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.scrollsToTop = NO;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kContentCellID];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        return _collectionView;
    }
    return _collectionView;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.childVcArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kContentCellID forIndexPath:indexPath];
    
    // 移除之前的子控件
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    // 添加控制器
    UIViewController *vc = self.childVcArray[indexPath.row];
    vc.view.frame = cell.contentView.bounds;
    
    if (self.style.isFullScreen) {
        CGFloat top = self.style.titleHeight;
        if ([vc isKindOfClass:[UITableViewController class]]) {
            UITableViewController *tableViewVc = (UITableViewController *)vc;
            tableViewVc.tableView.contentInset = UIEdgeInsetsMake(top, 0, 0, 0);
        } else if ([vc isKindOfClass:[UICollectionViewController class]]) {
            UICollectionViewController *collectionViewVc = (UICollectionViewController *)vc;
            collectionViewVc.collectionView.contentInset = UIEdgeInsetsMake(top, 0, 0, 0);
        }
    }
    
    [cell.contentView addSubview:vc.view];
    
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
}

#pragma mark -
#pragma mark UIScrollViewDelegate
// 减速完成
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger offsetXInt = offsetX;
    NSInteger screenWInt = self.collectionView.ys_width;
    
    NSInteger extre = offsetXInt % screenWInt;
    if (extre > self.collectionView.ys_width * 0.5) { // 往右边移动
        offsetX = offsetX + (self.collectionView.ys_width - extre);
        _isAniming = YES;
        [self.collectionView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    } else if (extre < self.collectionView.ys_width * 0.5 && extre > 0) { // 往左边移动
        _isAniming = YES;
        offsetX =  offsetX - extre;
        [self.collectionView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    }
    
    // 获取角标
    NSInteger index = offsetX / self.collectionView.ys_width;
    
    // 2. 通知TitleView进行调整
    [self.delegate contentView:self targetIndex:index];
}

// 监听滚动动画是否完成
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    _isAniming = NO;
}

// 停止拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (_isAniming) return;
    
    // 获取偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 获取左边角标
    NSInteger targetIndex = offsetX / self.collectionView.ys_width;
        
    [self.delegate contentView:self targetIndex:targetIndex offsetX:offsetX];
}

#pragma mark -
#pragma mark YSTitleViewDelegate
- (void)titleView:(YSTitleView *)titleView targetIndex:(NSInteger)targetIndex {
    CGFloat offsetX = targetIndex * self.collectionView.ys_width;
    self.collectionView.contentOffset = CGPointMake(offsetX, 0);
}

@end


